<?php
/**
 * 支付方式
 *
 *
 *
*/
defined('IN_NWIDC') or exit('Access Invalid!');
class paymentControl extends SystemControl{
	public function __construct(){
		parent::__construct();
		Language::read('payment');
	}

	/**
	 * 支付方式
	 */
	public function indexOp(){
		$model_payment = Model('payment');
		$payment_list = $model_payment->getPaymentList(array('payment_code'=>array('neq','predeposit')));
		Tpl::output('payment_list',$payment_list);
		Tpl::showpage('payment.list');
	}

	public function ini_file($inifilename,$key,$value=null) {
		if(!file_exists($inifilename)) exit("文件不存在");

		$confarr = parse_ini_file($inifilename,true);

		$newini = '';

		foreach ($confarr as $k => $v)
		{
			if($k==$key){
				$v = $value;
			}
			$newini .= $k."=".$v."\r\n";
		}

		if ( ($fi = fopen($inifilename,"w")) )
		{
			flock($fi, LOCK_EX);//排它锁
			fwrite($fi, $newini);
			flock($fi, LOCK_UN);
			fclose($fi);
			return true;
		}
		exit("没有写入权限");//写文件失败
	}

	/**
	 * 编辑
	 */
	public function editOp(){
		$model_payment = Model('payment');
		if (chksubmit()){
			$payment_id = intval($_POST["payment_id"]);
			$payment_code = $_POST['payment_code'];
			$data = array();
			$data['payment_state'] = intval($_POST["payment_state"]);

			$payment_config	= '';
			$config_array = explode(',',$_POST["config_name"]);//配置参数
			if(is_array($config_array) && !empty($config_array)) {
				$config_info = array();
				foreach ($config_array as $k) {
					$config_info[$k] = trim($_POST[$k]);
				}
				$payment_config	= serialize($config_info);
			}
			$data['payment_config'] = $payment_config;//支付接口配置信息

			if($payment_code=='abcpay'||$payment_code=='abcpay2'){

				if (count($_FILES)) {
					$path = dirname(BASE_PATH) . "/shop/api/payment/".$payment_code."/cert/merchant.pfx";
					@move_uploaded_file($_FILES['file']['tmp_name'],$path);
				}

				$ini_file = BASE_ROOT_PATH."/shop/api/payment/".$payment_code."/ebusclient/TrustMerchant.ini";
				$this->ini_file($ini_file,'MerchantID',$_POST['merchantid']);
				$this->ini_file($ini_file,'MerchantCertPassword',$_POST['merchantcertpassword']);
				$this->ini_file($ini_file,'TrustPayCertFile',BASE_ROOT_PATH."/shop/api/payment/".$payment_code."/cert/TrustPay.cer");
				$this->ini_file($ini_file,'MerchantCertFile',BASE_ROOT_PATH."/shop/api/payment/".$payment_code."/cert/merchant.pfx");
				$this->ini_file($ini_file,'LogPath',BASE_ROOT_PATH."/shop/api/payment/".$payment_code);

			}
				$model_payment->editPayment($data,array('payment_id'=>$payment_id));

			if($payment_id == 6){
				$str = file_get_contents(dirname(BASE_PATH) . '/wxpay/lib/WxPay.Config.php');
				$str = preg_replace("/#customstart[\s\S]+?#customend/","
	#customstart
	const APPID = '{$_POST['appid']}';
	const MCHID = '{$_POST['mch_id']}';
	const KEY = '{$_POST['key']}';
	const APPSECRET = '{$_POST['appsecret']}';
	#customend",$str
				);
				file_put_contents(dirname(BASE_PATH) . '/wxpay/lib/WxPay.Config.php',$str);

			}
			showMessage(Language::get('nc_common_save_succ'),'index.php?act=payment&op=index');
		}

		$payment_id = intval($_GET["payment_id"]);
		$payment = $model_payment->getPaymentInfo(array('payment_id'=>$payment_id));

		if($payment['payment_code']=='abcpay' || $payment['payment_code']=='abcpay2'){
			$payment_config = parse_ini_file(BASE_ROOT_PATH."/shop/api/payment/".$payment['payment_code']."/ebusclient/TrustMerchant.ini");
			$payment['payment_config'] = array(
				'merchantid'           => $payment_config['MerchantID'],
				'merchantcertpassword' => $payment_config['MerchantCertPassword']
			);

			Tpl::output('config_array',$payment['payment_config']);

		}
		else{
			if ($payment['payment_config'] != ''){
				Tpl::output('config_array',unserialize($payment['payment_config']));
			}
		}




		Tpl::output('payment',$payment);
		Tpl::showpage('payment.edit');
	}
}
